package action;

import bean.*;
import dao.*;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by WeaponLin on 2015/11/19.
 */
public class AdminAction extends SuperAction{

    public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
    public Date currentDate = Date.valueOf(dateFormat.format(System.currentTimeMillis()));

    public  String index(){
        return "index";
    }

    //图书信息列表
    public String queryBooks(){
        BookDAO bookDAO = new BookDAO();
        String loginState = null;
        String state = null;
        String op = null;
        List<Book> bookList = null;
        try{
            state = session.getAttribute("isAdmin").toString();  //是否是管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            op = request.getParameter("op");  //区分馆藏与借阅
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            if (op.equals("lib")) {  //查看馆藏图书
                bookList = bookDAO.queryBookBySql("bookid is not null");
                session.setAttribute("libBook_list", bookList);
                return "queryLibBooks_succ";
            }
            else if(op.equals("bor")){  //查看借阅图书
                bookList = bookDAO.queryBookBySql("quantityout > 0");
                session.setAttribute("borBook_list",bookList);
                return "queryBorBooks_succ";
            }
            else
                return "fail";
        } else{
            if (op.equals("lib")) {  //馆藏图书
                return "queryLibBooks_fail";
            }
            else if(op.equals("bor")){  //借阅图书
                return "queryBorBooks_fail";
            }
            else
                return "fail";
        }
    }

    //图书详情
    public String bookDetail(){
        BookDAO bookDAO = new BookDAO();
        BorrowDAO borrowDAO = new BorrowDAO();
        String state = null;
        String loginState = null;
        String bookid = null;
        String op = null;

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员登录
            loginState = session.getAttribute("loginState").toString();//是否登录
            bookid = request.getParameter("bookid");   //图书id
            op=request.getParameter("op");  ////区分馆藏与借阅
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")){  //管理员登录
            List<Book> bookList = bookDAO.queryBookBySql("bookid='" + bookid + "'");
            if(bookList.size() > 0 && op.equals("lib")){   //馆藏图书详情
                session.setAttribute("libBook_detail",bookList);
                return "libBookDetail_succ";
            } else if(bookList.size() > 0 && op.equals("bor")){  //借阅图书详情
                session.setAttribute("borBook_detail",bookList);
                List<Borrow> borrowList = borrowDAO.queryBorrowBySql("bookid='" + bookid + "' and dateactual is null");
                session.setAttribute("borReader_list",borrowList);
                return "borBookDetail_succ";
            }
            else
                return "fail";
        }  else{
            if (op.equals("lib")) {  //馆藏图书
                return "libBookDetail_fail";
            }
            else if(op.equals("bor")){  //借阅图书
                return "borBookDetail_fail";
            }
            else
                return "fail";
        }
    }

    //读者查询
    public String searchReader(){
        String state = null;
        String loginState = null;
        String readerid = null;
        String readername = null;
        String sql = "";
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
            readername = request.getParameter("readername");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            if (readerid != null && !readerid.trim().equals("") ) {
                sql += "readerid ='" + readerid + "' and ";
            }
            if ( readername != null && !readername.trim().equals("")) {
                sql += "readername like'%" + readername + "%' and ";
            }
            if(sql.equals("")){  //没有输入查询条件
                sql = "readerid is null";
            }
            else
                sql += "readerid is not null";
            ReaderDAO readerDAO = new ReaderDAO();
            List<Reader> readerList = readerDAO.queryReaderBySql(sql);
            session.setAttribute("readerSearch_result",readerList);
            return "searchReader_succ";
        }
        else {
            return "searchReader_fail";
        }
    }

    //读者列表
    public String readerList(){
        String state = null;
        String loginState = null;

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            return "readerList_fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            ReaderDAO readerDAO = new ReaderDAO();
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid is not null");
            session.setAttribute("reader_list", readerList);
            return "readerList_succ";
        }
        else
            return "readerList_fail";
    }

    //读者信息更新准备
    public String preUpdateReader(){
        String state = null;
        String loginState = null;
        String readerid = null;
        String sql = "";
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            return "preUpdate_fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            ReaderDAO readerDAO = new ReaderDAO();
            UserDAO userDAO = new UserDAO();
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid='"+readerid+"'");
            List<Users> userList = userDAO.queryUserByName(readerid);
            if(readerList.size() > 0)
                session.setAttribute("oldReader",readerList.get(0));
            if(userList.size() > 0)
                session.setAttribute("oldUser",userList.get(0));
            return "preUpdate_succ";
        }
        else{
            return "preUpdate_fail";
        }
    }

    //读者信息更新
    public String updateReader(){
        String state = null, loginState = null;
        Users oldUser = null;
        Reader oldReader = null;
        String readername = null,oldPassword = null,newPassword = null, rePassword = null, sex=null, birthday=null,
                phone=null, mobile=null,cardname=null,cardid=null,level=null;
        ReaderDAO readerDAO = new ReaderDAO();
        UserDAO userDAO = new UserDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            oldReader = (Reader) session.getAttribute("oldReader");
            oldUser = (Users) session.getAttribute("oldUser");
            readername = request.getParameter("readername");
            oldPassword = request.getParameter("oldPassword");
            newPassword = request.getParameter("newPassword");
            rePassword = request.getParameter("rePassword");
            sex = request.getParameter("sex");
            birthday = request.getParameter("birthday");
            phone = request.getParameter("phone");
            mobile = request.getParameter("mobile");
            cardname = request.getParameter("cardname");
            cardid = request.getParameter("cardid");
            level = request.getParameter("level");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "updateReader_fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            if(oldPassword != null && !oldPassword.trim().equals("")){
                if(!oldPassword.equals(oldUser.getPassword())){
                    this.addFieldError("oldPasswordError", "旧密码输入错误.");
                    return "updateReader_fail";
                }

                else {
                    if(newPassword == null || newPassword.equals("") || rePassword == null || rePassword.equals("")||
                            !newPassword.equals(rePassword)){
                        this.addFieldError("verifyPasswordError", "请输入新密码.");
                        return "updateReader_fail";
                    }
                    else{
                        oldUser.setPassword(newPassword);
                        userDAO.updateUser(oldUser);
                    }
                }
            }

            if(readername != oldReader.getReadername()){
                oldReader.setReadername(readername);
            }

            if(sex != oldReader.getSex()){
                oldReader.setSex(sex);
            }

            if(birthday == null || birthday.equals("")){
                this.addFieldError("birthdayError", "请输入出生年月日.");
                return "updateReader_fail";
            }
            java.sql.Date date = java.sql.Date.valueOf(birthday);
            if(date != oldReader.getBirthday()){
                oldReader.setBirthday(date);
            }

            if(phone != oldReader.getPhone()){
                oldReader.setPhone(phone);
            }

            if(mobile != oldReader.getMobile()){
                oldReader.setMobile(mobile);
            }


            if(cardname != oldReader.getCardname()){
                oldReader.setCardname(cardname);
            }

            if(cardid != oldReader.getCardid()){
                oldReader.setCardid(cardid);
            }

            if(!level.equals(oldReader.getMemberLevel().getLevel())){
                MemberLevel memberLevel = new MemberLevelDAO().queryLevelBySql("level='"+level+"'").get(0);
                oldReader.setMemberLevel(memberLevel);
            }
            if(readerDAO.updateReader(oldReader))
                return "updateReader_succ";
            else
                return "fail";
        }
        else{
            return "updateReader_fail";
        }
    }

    //办理借阅证
    public String addReader(){
        String state = null;
        String loginState = null;
        String readername = null,password = null, sex=null, birthday=null, phone=null, mobile=null,cardname=null,
                cardid=null,level=null;
        ReaderDAO readerDAO = new ReaderDAO();
        DecimalFormat decimalFormat = new DecimalFormat("0000000");
        UserDAO userDAO = new UserDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readername = request.getParameter("readername");
            password = request.getParameter("password");
            sex = request.getParameter("sex");
            birthday = request.getParameter("birthday");
            phone = request.getParameter("phone");
            mobile = request.getParameter("mobile");
            cardname = request.getParameter("cardname");
            cardid = request.getParameter("cardid");
            level = request.getParameter("level");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            return "addReader_fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Reader> readerList = readerDAO.queryReaderBySQL("select top 1 * from reader order by readerid desc");
            if(readerList.size() > 0){
                String readerid = readerList.get(0).getReaderid();
                int id = Integer.parseInt(readerid.substring(1))+1;
                String newid = "r"+decimalFormat.format(id);

                Reader addReader = new Reader();
                addReader.setReaderid(newid);
                addReader.setReadername(readername);
                addReader.setSex(sex);
                addReader.setBirthday(java.sql.Date.valueOf(birthday));
                addReader.setPhone(phone);
                addReader.setMobile(mobile);
                addReader.setCardid(cardid);
                addReader.setCardname(cardname);
                MemberLevel memberLevel = new MemberLevelDAO().queryLevelBySql("level='"+level+"'").get(0);
                addReader.setMemberLevel(memberLevel);
                addReader.setRegisterday(currentDate);
                addReader.setIsloss("否");

                Users addUser = new Users();
                addUser.setUsername(newid);
                addUser.setPassword(password);
                addUser.setState(0);
                if(readerDAO.queryReaderBySql("readername='"+readername+"'").size() > 0 ||
                        userDAO.queryUserByName(newid).size() > 0){
                    this.addFieldError("readerNameError", " 用户名已注册.");
                    return "addReader_fail";
                }
                if(readerDAO.addReader(addReader) && userDAO.addUser(addUser)){

                    return "addReader_succ";
                }
                else{
                    return "fail";
                }
            }
            return "fail";
        }
        else{
            return "addReader_fail";
        }
    }

    //删除读者
    public String deleteReader(){
        String state = null, loginState = null;
        String readerid = null;
        ReaderDAO readerDAO = new ReaderDAO();
        UserDAO userDAO = new UserDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            readerDAO.deleteReader(readerid);
            userDAO.deleteUser(readerid);
            return "deleteReader_succ";
        }
        else{
            return  "fail";
        }
    }

    //读者证挂失
    public String reportingLoss(){
        String state = null, loginState = null;
        String readerid = null;
        ReaderDAO readerDAO = new ReaderDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid='" + readerid + "'");
            if(readerList.size() > 0 && readerList.get(0).getIsloss().equals("否")){
                readerList.get(0).setIsloss("是");
                readerList.get(0).setLossdate(currentDate);
                readerDAO.updateReader(readerList.get(0));
                return "reportingLoss_succ";
            }
            else {
                this.addFieldError("reportingLossError","借阅证挂失失败");
                return "fail";
            }
        }
        else{
            this.addActionError("请使用管理员账号登录本系统！");
            return  "fail";
        }
    }

    //读者证解挂
    public String removingLoss(){
        String state = null, loginState = null;
        String readerid = null;
        ReaderDAO readerDAO = new ReaderDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }
        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid='" + readerid + "'");
            if(readerList.size() > 0 && readerList.get(0).getIsloss().equals("是")){
                readerList.get(0).setIsloss("否");
                readerList.get(0).setLossdate(null);
                readerDAO.updateReader(readerList.get(0));
                return "removingLoss_succ";
            }
            else {
                this.addFieldError("reportingLossError","借阅证解挂失败");
                return "fail";
            }
        }
        else{
            this.addFieldError("adminLoginError", "请使用管理员账号登录本系统！");
            return  "fail";
        }
    }

    public String preAddBook(){
        BookDAO bookDAO = new BookDAO();
        BookCategoryDAO bookCategoryDAO = new BookCategoryDAO();

        //查询类别列表
        List<BookCategory> categoryList = bookCategoryDAO.queryCategoryBySQL("categoryid is not NULL");
        session.setAttribute("category_list",categoryList);

        //出版社列表
        List<Book> bookList = bookDAO.queryBookBySql("bookid is not NULL");
        Set<String> publishing = new HashSet<String>();
        for (int i = 0;i < bookList.size();i++){
            publishing.add(bookList.get(i).getPublishing());
        }
        if(publishing.size() > 0){
            session.setAttribute("publishing_list",publishing);
            return "preAddBook_succ";
        }
        else {
            return "preAddBook_fail";
        }
    }

    //图书入库
    public String addBook(){
        String state = null, loginState = null;
        ReaderDAO readerDAO = new ReaderDAO();
        String bookname = null, author = null, publishing = null, category = null;
        double price = 0;
        Date datein = null;
        int quantityin = 0;
        DecimalFormat decimalFormat = new DecimalFormat("000");
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            bookname = request.getParameter("bookname");
            author = request.getParameter("author");
            publishing = request.getParameter("publishing");
            category = request.getParameter("category");
            price = Double.parseDouble(request.getParameter("price"));
            quantityin = Integer.parseInt(request.getParameter("quantityin"));
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }
        catch (NumberFormatException e1){
            System.out.println(e1.getMessage());
            this.addFieldError("priceError","请输入正确的图书价格!");
            return "addBook_fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            BookDAO bookDAO = new BookDAO();
            List<Book> bookList = bookDAO.queryBookBySQL("select top 1 * from Book order by bookid desc");
            BookCategoryDAO categoryDAO = new BookCategoryDAO();
            List<BookCategory> categoryList = categoryDAO.queryCategoryBySQL("category='"+category+"'");

            if(bookList.size() > 0){
                String bookid = bookList.get(0).getBookid();
                int id = Integer.parseInt(bookid.substring(1))+1;
                String newid = "b"+decimalFormat.format(id);
                Book book = new Book();
                book.setBookid(newid);
                book.setBookname(bookname);
                book.setAuthor(author);
                book.setPublishing(publishing);
                book.setBookCategory(categoryList.get(0));
                book.setPrice(price);
                book.setDatein(currentDate);
                book.setQuantityin(quantityin);
                book.setQuantityout(0);
                book.setQuantityloss(0);

                if(bookDAO.addBook(book))
                    return "addBook_succ";
                else
                    return "fail";
            }
            else
                return  "addBook_fail";
        }
        else{
            this.addFieldError("adminLoginError", "请使用管理员账号登录本系统！");
            return  "fail";
        }
    }

    //逾期未还
    public String timeoutReturn(){
        String state = null, loginState = null;
        BorrowDAO borrowDAO = new BorrowDAO();

        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("'"+currentDate+
                    "' > datereturn and dateactual is NULL and loss='否'");
            for (int i=0;i < borrowList.size();i++){
                long day = (currentDate.getTime() - borrowList.get(i).getDatereturn().getTime())/(3600*24*1000);
                borrowList.get(i).setDay(day);
            }
            if(borrowList.size() > 0)
                session.setAttribute("timeoutReturn_list",borrowList);
            else
                session.setAttribute("timeoutReturn_list",null);
            return "timeoutReturn_succ";
        }
        else{
            return  "timeoutReturn_fail";
        }
    }

    //图书遗失准备
    public String preBookLoss(){
        String state = null, loginState = null;
        BorrowDAO borrowDAO = new BorrowDAO();
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("dateactual is NULL and loss='否'");
            if(borrowList.size() > 0)
                session.setAttribute("searchBorrow_result",borrowList);
            else
                session.setAttribute("searchBorrow_result",null);
            return "preBookLoss_succ";
        }
        else{
            return  "preBookLoss_fail";
        }
    }

    //图书遗失
    public String bookLoss(){
        String state = null, loginState = null;
        BorrowDAO borrowDAO = new BorrowDAO();
        BookDAO bookDAO = new BookDAO();
        String bookid = null, readerid = null;
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            bookid = request.getParameter("bookid");
            readerid = request.getParameter("readerid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("dateactual is NULL and bookid='"+
                    bookid+"' and readerid='"+readerid+"'");
            List<Book> bookList = bookDAO.queryBookBySql("bookid='"+bookid+"'");
            if(borrowList.size() > 0 && bookList.size() > 0){
                int quantityLoss = bookList.get(0).getQuantityloss();
                int quantityOut = bookList.get(0).getQuantityout();

                if(quantityOut >= quantityLoss+1){
                    borrowList.get(0).setLoss("是");
                    bookList.get(0).setQuantityloss(quantityLoss+1);
                    borrowDAO.updateBorrow(borrowList.get(0));
                    bookDAO.updateBook(bookList.get(0));
                    return "bookLoss_succ";
                }
                else
                    return "fail";
            }
            else
                return "bookLoss_fail";
        }
        else{
            return  "bookLoss_fail";
        }
    }

    //图书借阅准备
    public String preBookBorrow(){
        String state = null, loginState = null;
        BookDAO bookDAO = new BookDAO();
        ReaderDAO readerDAO = new ReaderDAO();
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Book> bookList = bookDAO.queryBookBySql("bookid is not null");
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid is not null");
            session.setAttribute("searchBook_result",bookList);
            session.setAttribute("searchReader_result",readerList);
            return "preBookBorrow_succ";
        }
        else{
            return  "preBookBorrow_fail";
        }
    }

    //图书借阅
    public String bookBorrow(){
        String state = null, loginState = null;
        BookDAO bookDAO = new BookDAO();
        ReaderDAO readerDAO = new ReaderDAO();
        BorrowDAO borrowDAO = new BorrowDAO();
        String bookid = null, readerid = null;
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
            bookid = request.getParameter("bookid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Reader> readerList = readerDAO.queryReaderBySql("readerid='"+readerid+"'");
            List<Book> bookList = bookDAO.queryBookBySql("bookid='"+bookid+"'");
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("readerid='" + readerid + "' and dateactual is null and loss='否'");

            if(readerList.size() > 0 && bookList.size()> 0){
                Reader reader = readerList.get(0);
                Book book = bookList.get(0);
                if(reader.getIsloss().equals("是")){
                    this.addFieldError("readerIsLoss","读者证处于挂失状态，不可借阅图书.");
                    return "bookBorrow_fail";
                }
                if(book.getQuantityin() <= book.getQuantityout()){
                    this.addFieldError("noBookError","本书库存数量为0，不可借阅.");
                    return "bookBorrow_fail";
                }
                if(borrowList.size() >= reader.getMemberLevel().getLendnumbers()){
                    this.addFieldError("overLendNumber","读者借书数量已达上限.");
                    return "bookBorrow_fail";
                }
                book.setQuantityout(book.getQuantityout()+1);  //图书借阅数量加一
                Borrow borrow = new Borrow();
                borrow.setBook(book);
                borrow.setReader(reader);
                borrow.setLoss("否");
                borrow.setDateborrow(currentDate);
                Calendar cal = Calendar.getInstance();
                // 开始的日期相对于1970年1月1日的毫秒数+天数的毫秒数
                long lenddays = reader.getMemberLevel().getLenddays();
                long day = currentDate.getTime()+ lenddays * 3600 * 24 * 1000;
                cal.setTimeInMillis(day);
                String datereturn = dateFormat.format(cal.getTime());
                borrow.setDatereturn(java.sql.Date.valueOf(datereturn));
                borrow.setDateactual(null);

                borrowDAO.addBorrow(borrow);
                bookDAO.updateBook(book);
                this.addFieldError("borrowSuccess","图书借阅成功.");
                return "bookBorrow_succ";
            }
            else
                return "fail";
        }
        else{
            return  "bookBorrow_fail";
        }
    }

    //图书归还准备
    public String preBookReturn(){
        String state = null, loginState = null;

        BorrowDAO borrowDAO = new BorrowDAO();
        String bookid = null, readerid = null;
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("dateactual is null and loss='否'");
            if (borrowList.size() > 0)
                session.setAttribute("bookReturn_list",borrowList);
            else
                session.setAttribute("bookReturn_list",null);
            return "preBookReturn_succ";
        }
        else{
            return  "preBookReturn_fail";
        }
    }

    //图书归还
    public String bookReturn(){
        String state = null, loginState = null;
        BookDAO bookDAO = new BookDAO();
        ReaderDAO readerDAO = new ReaderDAO();
        BorrowDAO borrowDAO = new BorrowDAO();
        String bookid = null, readerid = null;
        try{
            state = session.getAttribute("isAdmin").toString();  //是否管理员
            loginState = session.getAttribute("loginState").toString();//是否登录
            readerid = request.getParameter("readerid");
            bookid = request.getParameter("bookid");
        }
        catch (NullPointerException e){
            System.out.println(e.getMessage());
            this.addFieldError("loginError","请登录本系统！");
            return "fail";
        }

        if(state.equals("true") && loginState.equals("1")) {  //管理员登录
            List<Borrow> borrowList = borrowDAO.queryBorrowBySql("readerid='"+readerid+"' and bookid='"+bookid+"' and dateactual is null");
            if(borrowList.size() > 0){

                Borrow borrow = borrowList.get(0);
                borrow.setDateactual(currentDate);
                int quantity = borrow.getBook().getQuantityout()-1;
                borrow.getBook().setQuantityout(quantity);
                if(borrowDAO.updateBorrow(borrow) && bookDAO.updateBook(borrow.getBook())){
                    return "bookReturn_succ";
                }
                else
                    return "fail";
            }
            else
                return "fail";
        }
        else{
            return  "bookReturn_fail";
        }
    }
}
